package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.ReportService;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 数据报告控制器
 */
@RestController
@RequestMapping("/report")
@Slf4j
@Api(tags = "数据报告展示接口")
public class ReportController {
    @Autowired
    private ReportService reportService;

//    @GetMapping("/getMemberReport")
//    public Result getMemberReport(){
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.MONTH,-12);
//    }

//    @GetMapping("/getSetmealReport")
//    public Result getSetmealReport() {
//        log.info("[报告-套餐饼状图]");
//        // 调用服务获取运营统计结果
//        HashMap<String, Object> data = reportService.getSetmealReport();
//        // 封装数据返回
//        if (data == null || data.size() < 1) {
//            return new Result(false, MessageConst.GET_SETMEAL_COUNT_REPORT_FAIL);
//        }
//        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, data);
//    }


    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        try {
            Map<String,Object> result = reportService.getBusinessReportData();
            return new Result(true,MessageConst.GET_BUSINESS_REPORT_SUCCESS,result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @GetMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletResponse response) {

        XSSFWorkbook workbook = reportService.exportBusinessReport();
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("report.xlsx", "utf-8"));
            BufferedOutputStream bout = new BufferedOutputStream(response.getOutputStream());
            workbook.write(bout);
            bout.flush();//清空缓冲区
            bout.close();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/getMemberReport")
    public Result getMemberReport() {
        log.info("[报高-会员人数折线图]");
        // 调用服务获取人数月数map
        HashMap<String, Object> data = reportService.getMemberReport();
        // 封装数据返回
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, data);
    }

    @GetMapping("/getSetmealReport")
    public Result getSetmealReport() {
        log.info("[报告-套餐饼状图]");
        // 调用服务获取运营统计结果
        HashMap<String, Object> data = reportService.getSetmealReport();
        // 封装数据返回
        if (data == null || data.size() < 1) {
            return new Result(false, MessageConst.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, data);
    }
}





